package com.mysoft.gfyx.marketdevelop.service.impl;

import com.mysoft.framework.common.exception.BusinessLogicException;
import com.mysoft.framework.modeling.dto.Filter;
import com.mysoft.framework.modeling.dto.SqlCondition;
import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.marketdevelop.mip.GetDeveloperDataService;
import com.mysoft.gfyx.marketdevelop.mip.agent.GetDeveloperDataServiceAgent;
import com.mysoft.gfyx.marketdevelop.mip.agent.dto.InvenstmentRelationCacheDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.BasicYcgListWithoutTotalResponseDto;
import com.mysoft.gfyx.marketdevelop.mip.dto.GetInvestmentRequestDto;
import com.mysoft.gfyx.marketdevelop.service.DeveloperService;
import com.mysoft.gfyx.marketdevelop.service.LoadDataApiCommonService;
import com.mysoft.gfyx.marketdevelop.service.LoadDataApiFilterChangerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LoadDataApiCommonServiceImp extends AppService implements LoadDataApiCommonService {

    @Autowired
    private GetDeveloperDataService getDeveloperDataService;

    @Autowired
    private GetDeveloperDataServiceAgent getDeveloperDataServiceAgent;

    public void setDefaultDeveloper(List<Filter> filters, String creditCode) {
        if (!this.checkIsSetDefaultDevelper(filters)) {
            Filter filter = new Filter();
            filter.setType("and");
            ArrayList<SqlCondition> conditions = new ArrayList<>();
            SqlCondition condition = new SqlCondition();
            condition.setOperatorType("in");
            condition.setField("organizing_code");

            InvenstmentRelationCacheDto res = getDeveloperDataServiceAgent.getFormativeInvestment(creditCode);

            ArrayList<String> conditionValue = new ArrayList<>();
            res.getList().forEach(i -> {
                if (i.getCreditCode() == null) {
                    return;
                }
                conditionValue.add(i.getCreditCode().toString());
            });
            if (conditionValue.size() == 0) {
                // throw new BusinessLogicException("该公司股权结构查询失败，无法展示数据");
                conditionValue.add(creditCode);
            }
            condition.setValue(conditionValue);

            conditions.add(condition);
            filter.setCondition(conditions);

            filters.add(filter);
        }
    }

    public void setSingleFilter(List<Filter> filters, String field, String value, String operate) {
        Filter filter = new Filter();
        filter.setType("and");
        ArrayList<SqlCondition> conditions = new ArrayList<>();
        SqlCondition condition = new SqlCondition();
        condition.setOperatorType(operate);
        condition.setField(field);
        condition.setValue(value);

        conditions.add(condition);
        filter.setCondition(conditions);

        filters.add(filter);
    }

    protected boolean checkIsSetDefaultDevelper(List<Filter> filters) {
        if (filters.size() == 0) {
            return false;
        }

        boolean flag = false;
        for (int f1 = 0; f1 < filters.size(); f1++) {
            for (int f2 = 0; f2 < filters.get(f1).getCondition().size(); f2++) {
                if ("organizing_code".equals(filters.get(f1).getCondition().get(f2).getField())) {
                    flag = true;
                    break;
                }
            }
        }

        return flag;
    }

    public void setDefaultDeveloperWithoutInvestment(List<Filter> filters, String creditCode) {
        creditCode = getCreditCodeWhenRemovingDeveloperFilter(filters, creditCode);

        Filter filter = new Filter();
        filter.setType("and");
        ArrayList<SqlCondition> conditions = new ArrayList<>();
        SqlCondition condition = new SqlCondition();
        condition.setOperatorType("eq");
        condition.setField("organizing_code");
        condition.setValue(creditCode);

        conditions.add(condition);
        filter.setCondition(conditions);

        filters.add(filter);
    }

    protected String getCreditCodeWhenRemovingDeveloperFilter(List<Filter> filters, String creditCode) {
        if (filters.size() == 0) {
            return creditCode;
        }

        for (int f1 = 0; f1 < filters.size(); f1++) {
            for (int f2 = 0; f2 < filters.get(f1).getCondition().size(); f2++) {
                if ("organizing_code".equals(filters.get(f1).getCondition().get(f2).getField())) {
                    try {
                        ArrayList<String> creditCodedList = (ArrayList<String>) filters.get(f1).getCondition().get(f2).getValue();
                        creditCode = creditCodedList.get(0);
                    } catch (Exception e) {}
                    finally {
                        filters.remove(f1);
                    }
                    break;
                }
            }
        }

        return creditCode;
    }

    public void filterChange(List<Filter> filters, LoadDataApiFilterChangerService changer) {
        if (filters.size() == 0) {
            return;
        }

        for (int f1 = 0; f1 < filters.size(); f1++) {
            for (int f2 = 0; f2 < filters.get(f1).getCondition().size(); f2++) {
                changer.change(filters.get(f1).getCondition().get(f2));
            }
        }
    }

    public Object getValueByConditionField(List<Filter> filters, String field) {
        if (filters.size() == 0) {
            return null;
        }

        for (int f1 = 0; f1 < filters.size(); f1++) {
            for (int f2 = 0; f2 < filters.get(f1).getCondition().size(); f2++) {
                if (filters.get(f1).getCondition().get(f2).getField().equals(field)) {
                    return filters.get(f1).getCondition().get(f2).getValue();
                }
            }
        }

        return null;
    }
}
